import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 26727
 * Date: 2024-02-07
 * Time: 22:04
 */
public class Heap {

    public int[] elem;
    public int usedSize;

    public Heap() {
        this.elem = new int[10];
    }

    public void initElem(int[] array) {
        for (int i = 0; i < array.length; i++) {
            elem[i] = array[i];
            usedSize++;
        }
    }
    //不是核心代码，就传10个以内的数据，如果非要写，可以扩容拷贝

    //大根堆
    //时间复杂度 O(N)
    public void createHeap1() {
        for (int parent = (usedSize-1-1)/2; parent >= 0; parent--) {
            shiftDown1(parent, usedSize);
        }
    }

    public void shiftDown1(int parent, int len) {
        int child = 2 * parent + 1;
        while(child < len) {
            if(child + 1 < len && elem[child] < elem[child+1]) {
                child++;
            }
            if(elem[child] > elem[parent]) {
                int tmp = elem[child];
                elem[child] = elem[parent];
                elem[parent] = tmp;
                parent = child;
                child = 2 * parent + 1;
            }else {
                break;
            }
        }
    }

    //小根堆
    public void createHeap2() {
        for(int parent = (usedSize-1-1)/2; parent >= 0; parent--) {
            shiftDown2(parent,usedSize);
        }
    }

    public void shiftDown2(int parent,int len) {
        int child = 2 * parent + 1;
        while(child < len) {
            if(child+1 < len && elem[child+1] < elem[child]) {
                child++;
            }
            if(elem[child] < elem[parent]) {
                int tmp = elem[child];
                elem[child] = elem[parent];
                elem[parent] = tmp;
                parent = child;
                child = 2 * parent + 1;
            }else {
                break;
            }
        }
    }



    //向上调整时间复杂度 O(N*logN)
    private void shiftUp(int child) {
        int parent = (child-1) / 2;
        while(child > 0) {
            if(elem[child] > elem[parent]) {
                int tmp = elem[child];
                elem[child] = elem[parent];
                elem[parent] = tmp;
                child = parent;
                parent = (child-1)/2;
            }else {
                break;
            }
        }

    }

    //插入
    public void offer(int val) {
        if(isFull()) {
            //扩容
            elem = Arrays.copyOf(elem,2*elem.length);
        }
        elem[usedSize] = val;
        usedSize++;
        shiftUp(usedSize-1);
    }

    private void swap(int[] array,int i,int j) {
        int tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
    }

    //删除(删除顶的元素)
    public void pop() {
        if(isEmpty()) {
            return;
        }

        int tmp = elem[0];
        elem[0] = elem[usedSize-1];
        elem[usedSize-1] = elem[0];

        //swap(elem,0,usedSize-1);

        usedSize--;

        shiftDown1(0,usedSize);
    }

    public boolean isEmpty() {
        return usedSize == 0;
    }

    public int peek() {
        if(isEmpty()){
            return -1;
        }
        return elem[0];
    }


    public boolean isFull() {

        return usedSize == elem.length;
    }
}
